package kubernetes

import (
	"fmt"
	"k8s-admin/global"
	"k8s-admin/service/kubernetes"
	"k8s-admin/utils"
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"
)

func GetClusterServiceLists(ctx *gin.Context) {
	params := new(struct {
		Namespace string `form:"namespace" binding:"required"`
	})
	cid := ctx.Param("cid")

	if err := ctx.ShouldBind(&params); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	} else {
		list, err := kubernetes.NewK8sInterface().GetClusterServiceList(cid, params.Namespace)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
			return
		}
		utils.ReturnContext(ctx).Successful("成功", list)
	}
}

func ServiceInfo(ctx *gin.Context) {
	params := new(struct {
		Namespace   string `form:"namespace" binding:"required"`
		ServiceName string `form:"service_name" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBind(&params); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	} else {

		list, err := kubernetes.NewK8sInterface().GetClusterServiceInfo(cid, params.Namespace, params.ServiceName)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
			return
		}
		utils.ReturnContext(ctx).Successful("成功", list)
	}
}

func CreateClusterService(ctx *gin.Context) {
	params := new(struct {
		Namespace   string `form:"namespace" json:"namespace" binding:"required"`
		Name        string `form:"name" json:"name" binding:"required"`
		Protocol    string `form:"protocol" json:"protocol" binding:"required"`
		ServiceType string `form:"service_type" json:"service_type" binding:"required"`
		Port        string `form:"port" json:"port" binding:"required"`
		TargetPort  string `form:"targetPort" json:"targetPort" binding:"required"`
		NodePort    string `form:"nodePort" json:"nodePort" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBindJSON(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())

	} else {

		err = kubernetes.NewK8sInterface().CreateClusterService(cid, params.Namespace, params.Name, params.Protocol, params.Port, params.TargetPort, params.ServiceType, params.NodePort)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "创建成功")
		}
	}
}

/*
*
使用yaml文件方式创建svc
*/
func CreateClusterServiceYaml(ctx *gin.Context) {
	cid := ctx.Param("cid")
	params := new(struct {
		Namespace string `form:"namespace"  json:"namespace" binding:"required"`
		YamlData  string `form:"yaml_data" json:"yaml_data" binding:"required"`
	})
	if err := ctx.ShouldBindJSON(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		err = kubernetes.NewK8sInterface().CreateClusterServiceYaml(cid, params.Namespace, params.YamlData)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "创建成功")
		}
	}
}

/**
删除svc
*/

func DeleteK8sClusterService(ctx *gin.Context) {
	cid := ctx.Param("cid")
	params := new(struct {
		Namespace   string `form:"namespace"  json:"namespace" binding:"required"`
		ServiceName string `form:"service_name" json:"service_name" binding:"required"`
	})
	if err := ctx.ShouldBind(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err)
	} else {
		err = kubernetes.NewK8sInterface().DeleteK8sClusterService(cid, params.Namespace, params.ServiceName)
		if err != nil {
			global.TPLogger.Error(err)

			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "删除成功")
		}
	}
}

/*
模板获取
*/
func GetClusterServiceYaml(ctx *gin.Context) {
	// /services/3b1fdefa53d996f443bc79f65a4bdc85/getClusterServiceYaml
	resource := strings.Split(ctx.Request.URL.String(), "/")[1]
	fmt.Println("URL请求资源地址：", resource)
	yaml, err := utils.ReadDeploymentYaml(resource)
	if err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		utils.ReturnContext(ctx).Successful("成功", yaml)
	}
}

/*
更新svc资源
*/
func UpdateClusterService(ctx *gin.Context) {
	params := new(struct {
		Namespace string `form:"namespace" json:"namespace" binding:"required"`
		YamlData  string `form:"yaml_data" json:"yaml_data" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBind(params); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	} else {
		err := kubernetes.NewK8sInterface().UpdateClusterService(cid, params.Namespace, params.YamlData)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "更新成功")
		}
	}
}
